package com.tarry.observer;

import java.util.ListIterator;
import java.util.Objects;
import java.util.Vector;

/**
 * teacher主题实现
 *
 */
public class TeacherSubject implements Subject {

    /**
     * 主题订阅者
     */
    private Vector<Observer> observers = new Vector<>();

    /**
     * 主题发布信息
     */
    private String info;

    @Override
    public void addObserver(Observer observer) {
       this.observers.add(observer);
    }

    @Override
    public void deleteObserver(Observer obj) {
        ListIterator<Observer> observerListIterator = this.observers.listIterator();
        while (observerListIterator.hasNext()){
            Observer observer = observerListIterator.next();
            if(Objects.equals(observer,obj))
                observerListIterator.remove();
        }
    }

    @Override
    public void notifyObserver() {
        this.observers.stream().parallel().forEach( observer -> observer.update(this.info));
    }

    public void setHomeWork(String info){
        this.info = info;
        System.out.println("今天的作业是：" + info);
        notifyObserver();
    }
}
